找传奇、传世资源到传世资源站!

C# 图像处理之灰度(去颜色的三种方法)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

去颜色的三种方法:内存法、指针法、像素提取法
from clipboardusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Diagnostics;namespace ImageProcessing{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } Stopwatch sw = new Stopwatch(); Bitmap bitmap; Bitmap newbitmap; private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { string path = openFileDialog1.FileName; bitmap = (Bitmap)Image.FromFile(path); pictureBox1.Image = bitmap.Clone() as Image; } } private void button4_Click(object sender, EventArgs e) { if (bitmap != null) { newbitmap = bitmap.Clone() as Bitmap; sw.Reset(); sw.Start(); Color pixel; int ret; for (int x = 0; x < newbitmap.Width; x ) { for (int y = 0; y < newbitmap.Height; y ) { pixel = newbitmap.GetPixel(x, y); ret = (int)(pixel.R * 0.299 pixel.G * 0.587 pixel.B * 0.114); newbitmap.SetPixel(x, y, Color.FromArgb(ret, ret, ret)); } } sw.Stop(); label1.Text = sw.ElapsedMilliseconds.ToString(); pictureBox2.Image = newbitmap.Clone() as Image; } } private void button5_Click(object sender, EventArgs e) { if (bitmap != null) { newbitmap = bitmap.Clone() as Bitmap; sw.Reset(); sw.Start(); Rectangle rect = new Rectangle(0, 0, newbitmap.Width, newbitmap.Height); System.Drawing.Imaging.BitmapData bmpdata = newbitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, newbitmap.PixelFormat); IntPtr ptr = bmpdata.Scan0; int bytes = newbitmap.Width * newbitmap.Height * 3; byte[] rgbvalues = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, rgbvalues, 0, bytes); double colortemp = 0; for (int i = 0; i < rgbvalues.Length; i = 3) { colortemp = rgbvalues[i 2] * 0.299 rgbvalues[i 1] * 0.587 rgbvalues[i] * 0.114; rgbvalues[i] = rgbvalues[i 1] = rgbvalues[i 2] = (byte)colortemp; } System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, ptr, bytes); newbitmap.UnlockBits(bmpdata); sw.Stop(); label2.Text = sw.ElapsedMilliseconds.ToString(); pictureBox2.Image = newbitmap.Clone() as Image; } } private void button6_Click(object sender, EventArgs e) { if (bitmap != null) { newbitmap = bitmap.Clone() as Bitmap; sw.Reset(); sw.Start(); Rectangle rect = new Rectangle(0, 0, newbitmap.Width, newbitmap.Height); System.Drawing.Imaging.BitmapData bmpdata = newbitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, newbitmap.PixelFormat); byte temp; unsafe { byte* ptr = (byte*)(bmpdata.Scan0); for (int x = 0; x < bmpdata.Width; x ) { for (int y = 0; y < bmpdata.Height; y ) { temp = (byte)(0.299 * ptr[2] 0.587 * ptr[1] 0.114 * ptr[0]); ptr[0] = ptr[1] = ptr[2] = temp; ptr = 3; } ptr = bmpdata.Stride - bmpdata.Width * 3; } } newbitmap.UnlockBits(bmpdata); sw.Stop(); label3.Text = sw.ElapsedMilliseconds.ToString(); pictureBox2.Image = newbitmap.Clone() as Image; } } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复